import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },

  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    // 订单管理模块
    path: '/order',
    component: Layout,
    redirect: '/order/index',
    children: [
      {
        path: 'index',
        name: 'Order',
        component: () => import('@/views/order/index'),
        meta: {
          title: '订单管理',
          icon: 'el-icon-tickets'
        }
      }]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user', noCache: true }
      }
    ]
  },

  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  }

]

export const asyncRoutes = [
  // 动态路由写法实例
  // {
  //   // 订单管理模块
  //   path: '/order',
  //   component: Layout,
  //   redirect: '/order/index',
  //   name: 'ordermanage',
  //   meta: {
  //     title: '订单管理',
  //     icon: 'el-icon-tickets',
  //     roles: ['ROLE_ADMIN', 'ROLE_ORDER']
  //   },
  //   children: [
  //     {
  //       // 订单列表，展示所有订单
  //       path: 'index',
  //       name: 'Order',
  //       component: () => import('@/views/order/index'),
  //       meta: {
  //         title: '订单列表',
  //         icon: 'el-icon-tickets',
  //         roles: ['ROLE_ADMIN', 'ROLE_ORDER']
  //       }
  //     }, {
  //       // 订单详情
  //       path: 'detail/:oid',
  //       name: 'OrderDetail',
  //       hidden: true,
  //       component: () => import('@/views/order/detail'),
  //       meta: {
  //         title: '订单详情',
  //         icon: 'el-icon-tickets',
  //         roles: ['ROLE_ADMIN', 'ROLE_ORDER']
  //       }
  //     }, {
  //       // 展示所有用户列表，然后查询某个用户的所有订单
  //       path: 'user',
  //       name: 'OrderUser',
  //       component: () => import('@/views/order/user'),
  //       meta: {
  //         title: '用户列表',
  //         icon: 'el-icon-user',
  //         roles: ['ROLE_ADMIN', 'ROLE_ORDER']
  //       }
  //     }, {
  //       // 展示所有用户列表，然后查询某个用户的所有订单
  //       path: 'userList/:uid',
  //       name: 'OrderUser',
  //       hidden: true,
  //       component: () => import('@/views/order/userOrderDetail'),
  //       meta: {
  //         title: '用户订单列表',
  //         icon: 'el-icon-tickets',
  //         roles: ['ROLE_ADMIN', 'ROLE_ORDER']
  //       }
  //     }]
  // }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 重置路由，恢复默认路由
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
